package cn.reone.mbt.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import com.orhanobut.logger.Logger;

import java.util.Arrays;

/**
 * Created by wangxingsheng on 2017/2/5.
 *
 * 这个工具类用于6.0之后 在运行时请求权限
 * 关于主动权限需要注意的是
 * 1.所有需要的权限,不管是否有主动请求操作,都需要在manifest中做配置
 * 2.权限有正常权限与危险权限:
 *      在manifest中列出了正常权限，系统将自动授予该权限;
 *      在manifest中列出了危险权限，则用户必须明确批准您的应用使用这些权限。
 *
 *
 * 更多见官方法介绍:https://developer.android.google.cn/training/permissions/requesting.html
 *
 */

public class PermissionUtil {

    public static final int REQUESTCODE_ACCESS_FINE_LOCATION = 10001;

    private static void log(String debug){
        Logger.d("PermissionUtil "+debug);
    }

    /**
     * 主动请求权限,按官方推荐逻辑,此方法应该在每次需要权限时都要调用
     * 检查权限->询问请求历史 ? 解释权限 (点击确认再次请求) : 直接请求权限
     * @param thisActivity
     * @param permission
     * @param requestCode
     * @param dialogMessage
     */
    public static void requestPermissionWithCheck(Activity thisActivity, String permission, int requestCode, String dialogMessage){

        if(Build.VERSION.SDK_INT < 23){
            log("your sdk < 23,the method 'requestPermissionWithCheck(..' do nothing");
            return;
        }
        try {
            //检查权限是否已经开启
            if (ContextCompat.checkSelfPermission(thisActivity,permission)
                    != PackageManager.PERMISSION_GRANTED) {
                log("the permission has not,continue...");
                //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
                //此时应该向用户解释为什么需要这个权限
                if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                        permission)) {
                    //弹出解释弹窗
                    showExplanation(thisActivity,requestCode,dialogMessage,permission);
                } else {
                    //发起权限请求
                    requestPermissions(thisActivity,new String[]{permission},requestCode);
                }
            }else{
                log("the permission has ready");
            }
        }catch (Exception e){
            log("the method 'requestPermissionWithCheck(..' apper error,so jump setting");
            //请求权限的过程中出现问题直接跳到设置界面,让用户手动设置权限
            openPermissionSetting(thisActivity);
        }

    }

    /**
     * 直接请求权限
     */
    public static void requestPermissions(Activity thisActivity,String[] permissions,int requestCode){
        log("request Permission " + Arrays.asList(permissions));
        ActivityCompat.requestPermissions(thisActivity,
                permissions,
                requestCode);
    }

    /**
     * 开启本应用权限设置界面
     * @param context
     */
    public static void openPermissionSetting(Context context){
        log("start jump Setting...");
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
        intent.setData(uri);
        context.startActivity(intent);
        log("jump Setting end");
    }

    /**
     * 弹出解释权限的弹框,点击确定请求权限
     * @param thisActivity
     * @param requestCode
     * @param dialogMessage
     * @param requestPermission
     */
    public static void showExplanation(final Activity thisActivity,final int requestCode,
                                            String dialogMessage, final String requestPermission) {
        log("here show showExplanation");
        String defaultMessage = "Need " + requestPermission + " authority, otherwise next function can not be used";
        showMessageOKCancel(thisActivity,
                dialogMessage == null ? defaultMessage : defaultMessage
                , new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                requestPermissions(thisActivity,new String[]{requestPermission},requestCode);
            }
        });
    }

    /**
     * 在onRequestPermissionsResult中判断某个requestCode陪同的权限请求结果是否成功
     *
     * @param grantResults
     * @return
     */
    public static boolean checkRequestCode(@NonNull int[] grantResults){
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //权限请求成功
            return true;
        } else {
            //权限请求被拒绝
            return false;
        }
    }

    /*此方法是在onRequestPermissionsResult中回调的例子
    public final static void requestPermissionsResult(final Activity activity,
                                                final int requestCode,
                                                @NonNull String[] permissions,
                                                @NonNull int[] grantResults) {
        if (activity == null) {
            return;
        }
        switch (requestCode) {
            case -1:
                if (PermissionUtil.checkRequestCode(grantResults)) {
                    //权限请求成功

                } else {
                    //权限请求被拒绝
                }
                break;
            default:
        }
    }
    */

        /**
         * 一个普通弹窗
         * @param context
         * @param message
         * @param okListener
         */
    private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();

    }

}
